#include <p18cxxx.h>
#include "hardware.h"
#include "datatypes.h"

/*
 * Wartet, bis USB verbunden ist
 */
void WtUSB() {
	while(USB_PWREN == 1){
	  // clear watchdogtimer
	  ClrWdt();	
	};
}

void waitRxUSB() {
	while(USB_RXF == 1) {
	  // clear watchdogtimer
	  ClrWdt();	
	}
}

void waitTxUSB() {
	while(USB_TXE == 1) {
	  // clear watchdogtimer
	  ClrWdt();	
	}
}

/*
 * Liest ein Byte vom USB-Port
 */
unsigned char receiveUSB() {
	unsigned char byte;

	TRISD = 0xff;		// switch Port D to Input
	LATEbits.LATE0 = 0;	// activate RD#
	Nop();
	byte = LATD;
	Nop();
	LATEbits.LATE0 = 1;	// deactivate RD#
	TRISD = 0x00;		// switch Port D back to Output	
	return byte;
}

/*
 * Schreibt ein Byte zum USB-Port
 */
void transmitUSB(unsigned char byte) {
	LATD = byte;
	Nop();
	USB_WR = 1;
	Nop();
	USB_WR = 0;
	Nop();
	LATD = 0x00;
}



void sendEmsUSBRaw() {
}

void sendEmsUSBHex() {
}
